﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Javavaj.iHunter;

using Javavaj.iHunter.Core;
namespace Javavaj.iHunter.UI
{
    internal partial class MessageEditForm : DevComponents.DotNetBar.Office2007Form
    {
        public MessageEditForm(KeyValuePair<string, EditItemType>[] editItems)
        {
            InitializeComponent();
            InitEditFields(editItems);
        }

        struct InputUIItem
        {
            public string name;
            public EditItemType type;
            public Control control;
        }
        List<InputUIItem> uiItems;

        void GetInputItems()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            foreach (InputUIItem uiItem in uiItems)
            {
                switch (uiItem.type)
                {
                    case EditItemType.SingleLineText:
                        {
                            DevComponents.DotNetBar.Controls.TextBoxX edit = uiItem.control as DevComponents.DotNetBar.Controls.TextBoxX;
                            dic.Add(uiItem.name, edit.Text);
                        }
                        break;
                    case EditItemType.MultiLineText:
                        {
                            DevComponents.DotNetBar.Controls.TextBoxX edit = uiItem.control as DevComponents.DotNetBar.Controls.TextBoxX;
                            dic.Add(uiItem.name, edit.Text);
                        }
                        break;
                    case EditItemType.FilePath:
                        {
                            DevComponents.DotNetBar.Controls.MaskedTextBoxAdv edit = uiItem.control as DevComponents.DotNetBar.Controls.MaskedTextBoxAdv;
                            dic.Add(uiItem.name, edit.Text);
                        }
                        break;
                    default:
                        break;
                }
            }
            inputItems = dic;

        }
        private IDictionary<string, object> inputItems;

        public IDictionary<string, object> InputItems
        {
            get
            {
                if (!this.IsDisposed)
                    GetInputItems();

                return inputItems;
            }
            set
            {
                if (this.IsDisposed)
                    throw new ObjectDisposedException("MessageEditForm");

                foreach (InputUIItem uiItem in uiItems)
                {
                    switch (uiItem.type)
                    {
                        case EditItemType.SingleLineText:
                            {
                                DevComponents.DotNetBar.Controls.TextBoxX edit = uiItem.control as DevComponents.DotNetBar.Controls.TextBoxX;
                                if (value.ContainsKey(uiItem.name))
                                    edit.Text = value[uiItem.name] as string;
                            }
                            break;
                        case EditItemType.MultiLineText:
                            {
                                DevComponents.DotNetBar.Controls.TextBoxX edit = uiItem.control as DevComponents.DotNetBar.Controls.TextBoxX;
                                if (value.ContainsKey(uiItem.name))
                                    edit.Text = value[uiItem.name] as string;
                            }
                            break;
                        case EditItemType.FilePath:
                            {
                                DevComponents.DotNetBar.Controls.MaskedTextBoxAdv edit = uiItem.control as DevComponents.DotNetBar.Controls.MaskedTextBoxAdv;
                                if (value.ContainsKey(uiItem.name))
                                    edit.Text = value[uiItem.name] as string;
                            }
                            break;
                        default:
                            break;
                    }
                }

            }
        }

        public event EventHandler EditCompleted;

        private void InitEditFields(KeyValuePair<string, EditItemType>[] editItems)
        {
            uiItems = new List<InputUIItem>();
            this.tableLayoutPanel1.SuspendLayout();

           this.tableLayoutPanel1.RowStyles.Clear();
           this.tableLayoutPanel1.ColumnStyles.Clear();
           this.tableLayoutPanel1.RowCount = 0;
           this.tableLayoutPanel1.ColumnCount = 2;
           this.tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 80));
           this.tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 0));
            
            int row = 0;
            foreach (KeyValuePair<string, EditItemType> item in editItems)
            {
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute,22));
                DevComponents.DotNetBar.LabelX label = new DevComponents.DotNetBar.LabelX()
                {
                    Dock = DockStyle.Fill,
                    Text = item.Key +":"
                };
                tableLayoutPanel1.Controls.Add(label, 0, row);


                switch (item.Value)
                {
                    case EditItemType.SingleLineText:
                        {
                            DevComponents.DotNetBar.Controls.TextBoxX edit = new DevComponents.DotNetBar.Controls.TextBoxX()
                            {
                                Tag = item.Key,
                                Dock =  DockStyle.Fill,
                                Multiline = false,
                            };
                            tableLayoutPanel1.Controls.Add(edit, 1,row);
                            uiItems.Add(new InputUIItem()
                            {
                                name = item.Key,
                                type = item.Value,
                                control = edit
                            });

                        }
                        break;

                    case EditItemType.MultiLineText:
                        {
                            tableLayoutPanel1.SetColumnSpan(label, 2);
                            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 120));

                            DevComponents.DotNetBar.Controls.TextBoxX edit = new DevComponents.DotNetBar.Controls.TextBoxX()
                            {
                                Tag = item.Key,
                                Dock = DockStyle.Fill,
                                Multiline = true,
                                ScrollBars = ScrollBars.Both
                            };


                            row++;
                            tableLayoutPanel1.Controls.Add(edit, 0, row);
                            tableLayoutPanel1.SetColumnSpan(edit, 2);
                            uiItems.Add(new InputUIItem()
                            {
                                name = item.Key,
                                type = item.Value,
                                control = edit
                            });
                        }

                        break;
                    
                    case EditItemType.FilePath:
                        {
                            DevComponents.DotNetBar.Controls.MaskedTextBoxAdv edit = new DevComponents.DotNetBar.Controls.MaskedTextBoxAdv()
                            {

                                Tag = item.Key,
                                Dock = DockStyle.Fill,
                                ReadOnly = true,

                            };

                            edit.ButtonCustom.Visible = true;
                            edit.ButtonCustomClick += delegate
                            {
                                if (openFileDialog.ShowDialog() == DialogResult.OK)
                                    edit.Text = openFileDialog.FileName;
                            };
                            tableLayoutPanel1.Controls.Add(edit, 1, row);
                            uiItems.Add(new InputUIItem()
                            {
                                name = item.Key,
                                type = item.Value,
                                control = edit
                            });

                        }

                        break;
                    case EditItemType.StaticText:
                        tableLayoutPanel1.SetColumnSpan(label, 2);
                        tableLayoutPanel1.RowStyles[row] = new RowStyle(SizeType.Absolute, 40);
                        break;
                    default:
                        break;
                }

                row++;
            }

            this.tableLayoutPanel1.ResumeLayout(true);
          
        }

        private void commandSend_Executed(object sender, EventArgs e)
        {
            if (EditCompleted != null)
                EditCompleted.Invoke(this, EventArgs.Empty);
            this.Close();
        }

        private void commandCancel_Executed(object sender, EventArgs e)
        {

            this.Close();
        }

        private void MessageEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            GetInputItems();

        }
    }
}
